<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\Request;
use think\facade\View;
use think\exception\ValidateException;


// 班级管理
class ClassesTotal extends BaseController
{


    public function index()
    {
        $teacher = Db::name('members')
            ->where('status', '=', 1)
            ->where('role', 'in', '2,3')
            ->field('id, userid, name')
            ->select();
        View::assign('teacher', $teacher);

        return View::fetch();
    }

    public function load(Request $request)
    {
        if(!check_jurisdiction(session('admin_auth.role_id'), $this->control, $this->action)) {
            return json(['code' => 500, 'data' => '', 'msg' => '无操作权限']);
        }

        $get = $request->get();
        $where = [];
        if (isset($get['time'])) {
            if (!empty($get['time'])) {
                $where[] = ['a.start', 'between', [strtotime($get['time']), strtotime($get['time'])+86399]];
            }
        }

        if (isset($get['keys'])) {
            if (!empty($get['keys'])) {
                $where[] = ['a.teacher_name|b.e_seminar', 'like', '%'.$get['keys'].'%'];
            }
        }
        if (isset($get['teacherid'])) {
            if (!empty($get['teacherid'])) {
                $where[] = ['','EXP',Db::raw('FIND_IN_SET("'.$get['teacherid'].'",a.teacherid)')];
            }
        }

        if (isset($get['status'])) {
            if (!empty($get['status'])) {
                if ($get['status'] == 1) {
                    $where[] = ['a.start', '>', time()];
                }elseif ($get['status'] == 2) {
                    $where[] = ['a.start', '<=', time()];
                    $where[] = ['a.end', '>=', time()];
                }elseif ($get['status'] == 3) {
                    $where[] = ['a.end', '<', time()];
                }
            }
        }

        $where[] = ['a.status', '=', 1];

        $total = Db::table('o_classes')
            ->alias('a')
            ->join('o_course b','a.courseid=b.courseid', 'left')
            ->where($where)
            ->count();

        $rows = Db::table('o_classes')
            ->alias('a')
            ->join('o_course b','a.courseid=b.courseid', 'left')
            ->where($where)
            ->field('a.id, a.teacherid, a.teacher_name, a.courseid, a.start, a.end, a.status, b.e_seminar')
            ->order('a.id', 'desc')
            ->page($get['page'], $get['limit'])
            ->select()
            ->toArray();

        foreach ($rows as &$v) {
            $v['time'] = date('Y-m-d H:i',$v['start']).' - '.date('Y-m-d H:i',$v['end']);

            if(empty($v['start']) && empty($v['end'])){
                $v['status_name'] = '<span style="color:orange;" >请设置开课时间</span>';
            }elseif ($v['start'] > time()) {
                $v['status_name'] = '<span>未开始</span>';
            }elseif(($v['start'] <= time()) && ($v['end'] >= time())) {
                $v['status_name'] = '<span style="color:green;">进行中</span>';
            }elseif($v['end'] < time()) {
                $v['status_name'] = '<span style="color:red;">已结束</span>';
            }
            $v['members_num'] = intval(Db::name('classes_user')->where('classes_id', '=', $v['id'])->count());

            $all =  intval(Db::name('classes_user')->where('classes_id', '=', $v['id'])->count());
            if ($all) {
                $q = intval(Db::name('classes_log')->where('classes_id', '=', $v['id'])->where('qiandao', '=', 1)->count());
                if ($q) {
                    $l = ($q/$all)*100;
                    $v['chuqin'] = round($l).'%';
                }else{
                    $v['chuqin'] = 0;
                }
            }else{
                $v['chuqin'] = 0;
            }
        }
        $data = array(
            'code'=> 0,
            'msg'=> '',
            'count' => $total,
            'data'=> $rows
        );
        return json($data);
    }
}